package com.coderlin.controller;

import com.coderlin.entities.Customer;
import com.coderlin.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/22 13:56 <br>
 * @see com.coderlin.controller <br>
 */
@Controller
@RequestMapping("/cust")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/listCustomer")
    public String listCustomer(){
        return customerService.listCustomer().toString();
    }

    @RequestMapping("/listCust")
    public String listCust(Model model){
        List<Customer> customers = customerService.listCustomer();
        model.addAttribute("custList",customers);
        return "custList";
    }

    @RequestMapping("/listCustPage")
    public String listCustrPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,Model model){
        PageInfo<Customer> pageInfo = customerService.listCustomerForPage(pageNum);
        List<Customer> custList = pageInfo.getList();
        model.addAttribute("custList",custList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("pageData","listData");
        return "custList";

    }
    @RequestMapping("/searchCustPage")
    public String searchCustPage(String custName,
                                 @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 Model model){
        PageInfo<Customer> pageInfo = customerService.searchCustomer(pageNum, custName);
        List<Customer> custList = pageInfo.getList();
        model.addAttribute("custList",custList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("pageData","searchData");
        model.addAttribute("data",custName);
        return "custList";
    }


    @RequestMapping("/searchCust")
    public String searchCust(String custName, Model model){
        List<Customer> customers = customerService.searchCustomer(custName);
        model.addAttribute("custList",customers);
        return "custList";
    }

    @RequestMapping("/addCust")
    public String addCust(Customer customer){
        Integer integer = customerService.addCustomer(customer);

        //判断是否添加成功，来进行弹窗
        if(integer<=0){

        }
        return "redirect:/cust/listCustPage";
    }

    @RequestMapping("/addCustPre")
    public String addCustPre(){
        return "custAdd";
    }

    @RequestMapping("/delCust/{cid}")
    public String delCust(@PathVariable("cid") int cid){
        Integer integer = customerService.delCustomerById(cid);
        //判断是否添加成功，来进行弹窗
        if(integer<=0){

        }
        return "redirect:/cust/listCustPage";
    }

    @RequestMapping("/modCustPre/{cid}")
    public String modCustPre(@PathVariable("cid") int cid,Model model){
        Customer customer = customerService.searchCustomerById(cid);
        model.addAttribute("customer",customer);
        model.addAttribute("cid",cid);
        return "custMod";
    }

    @RequestMapping("/modCust")
    public String modCust(Customer customer){
        Integer integer = customerService.modCustomer(customer);

        return "redirect:/cust/listCustPage";
    }

}
